package T7;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleShell {

    public static void main(String[] args) throws IOException {
        BufferedReader kbin = new BufferedReader(new InputStreamReader(System.in)); // 缓冲读取器

        // 无限循环，等待用户输入命令
        while (true) {
            System.out.print("# ");
            String cmd = kbin.readLine().trim();  // 读取用户输入并去除两端空白

            // 处理不同的命令
            if (cmd.equalsIgnoreCase("exit")) {
                // 退出程序
                System.exit(0);
            } else if (cmd.toLowerCase().startsWith("ls ")) {
                // 处理 ls 命令
                ls(cmd.substring(3).trim());
            } else if (cmd.toLowerCase().startsWith("cp ")) {
                // 处理 cp 命令
                String[] parts = cmd.substring(3).trim().split("\\s+");
                if (parts.length < 2) {
                    System.out.println("Usage: cp <source> <destination>");
                } else {
                    cp(parts[0], parts[1]);
                }
            } else if (cmd.toLowerCase().startsWith("mv ")) {
                // 处理 mv 命令
                String[] parts = cmd.substring(3).trim().split("\\s+");
                if (parts.length < 2) {
                    System.out.println("Usage: mv <source> <destination>");
                } else {
                    mv(parts[0], parts[1]);
                }
            } else if (cmd.toLowerCase().startsWith("rm ")) {
                // 处理 rm 命令
                String filePath = cmd.substring(3).trim();
                rm(filePath);
            } else {
                System.out.println("无法识别所输入命令: " + cmd);
            }
        }
    }

    // ls 命令：列出指定目录的内容
    private static void ls(String path) {
        File dir = new File(path);
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("路径不存在或不是目录: " + path);
            return;
        }

        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                // 格式化文件的最后修改时间
                Date lastModified = new Date(file.lastModified());
                String dateFormatted = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lastModified);
                System.out.printf("%s\t", dateFormatted);

                if (file.isDirectory()) {
                    System.out.printf("%-16s\t", "<DIR>");
                } else {
                    System.out.printf("%16d\t", file.length());
                }

                System.out.println(file.getName());
            }
        } else {
            System.out.println("无法列出目录内容: " + path);
        }
    }

    // cp 命令：复制文件
    private static void cp(String sourcePath, String destPath) {
        File source = new File(sourcePath);
        File dest = new File(destPath);

        if (!source.exists()) {
            System.out.println("源文件不存在: " + sourcePath);
            return;
        }

        try (InputStream in = new FileInputStream(source);
             OutputStream out = new FileOutputStream(dest)) {

            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            System.out.println("文件已复制: " + sourcePath + " 到 " + destPath);
        } catch (IOException e) {
            System.out.println("复制文件时出错: " + e.getMessage());
        }
    }

    // mv 命令：移动文件
    private static void mv(String sourcePath, String destPath) {
        File source = new File(sourcePath);
        File dest = new File(destPath);

        if (!source.exists()) {
            System.out.println("源文件不存在: " + sourcePath);
            return;
        }

        if (source.renameTo(dest)) {
            System.out.println("文件已移动: " + sourcePath + " 到 " + destPath);
        } else {
            System.out.println("移动文件失败: " + sourcePath);
        }
    }

    // rm 命令：删除文件
    private static void rm(String filePath) {
        File file = new File(filePath);

        if (!file.exists()) {
            System.out.println("文件不存在: " + filePath);
            return;
        }

        if (file.delete()) {
            System.out.println("文件已删除: " + filePath);
        } else {
            System.out.println("删除文件失败: " + filePath);
        }
    }
}
